package com.jcnet.bizaia.httpRequest;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.Header;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.jcnet.bizaia.App;
import com.jcnet.bizaia.DiscussSearchActivity;
import com.jcnet.bizaia.R;
import com.jcnet.bizaia.bean.CatgoryBean;
import com.jcnet.bizaia.tools.Toolkit;
import com.jcnet.bizaia.view.ProgressHUD;
import com.jcnet.bizaia.xstream.ACache;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import android.content.Context;
import android.content.Intent;
import android.widget.Toast;

public class CategoryHttpRequstTools {

	/**
	 * 得到全部分类
	 */

	private ACache aCache;
	
	private Context context;
	
	private RequestParams params;
	
	private List<CatgoryBean> catgoryBeans;
	
	private static final String CATEGORY_HTTP_REQUEST = App.HTTP_PATH+"clas_showClassifyList.action?";
	
	private ProgressHUD progressHUD;

	
	private String requestString = null;
	
	public CategoryHttpRequstTools(Context context,RequestParams params) {
		super();
		this.context = context;
		this.params = params;
		
		aCache = ACache.get(context);
		
		progressHUD = ProgressHUD.show(context, context.getString(R.string.dataloding), false, true, null);
		catgoryBeans = new ArrayList<CatgoryBean>();
	}

	public List<CatgoryBean> getAllCategory(){
		
		//requestString = aCache.getAsString(CATEGORY_HTTP_REQUEST+params).toString();
		
		if(Toolkit.isAvaliable(context)&&requestString==null){
			//有网络请求数据
			new AsyncHttpClient().post(CATEGORY_HTTP_REQUEST, params, new AsyncHttpResponseHandler(){
				
				@Override
				public void onStart() {
					// TODO Auto-generated method stub
					super.onStart();
					
					progressHUD.show();
					
				}
				
				
				@Override
				public void onSuccess(int arg0, Header[] arg1, byte[] arg2) {
					// TODO Auto-generated method stub
					super.onSuccess(arg0, arg1, arg2);
					
					String requestString  = new String(arg2);
					if(requestString!=null){
						aCache.put(CATEGORY_HTTP_REQUEST+params, requestString,App.CACHE_TIME_DAY_VERY_SHORT);
						setListData(requestString);
					}
					
					
				}
				
				@Override
				public void onFailure(int arg0, Header[] arg1, byte[] arg2,
						Throwable arg3) {
					// TODO Auto-generated method stub
					super.onFailure(arg0, arg1, arg2, arg3);
					if(progressHUD.isShowing()){
						progressHUD.cancel();
					}
					
					Toast.makeText(context, context.getString(R.string.serverError), 0).show();
				}
				
				
				@Override
				public void onFinish() {
					// TODO Auto-generated method stub
					super.onFinish();
					if(progressHUD.isShowing()){
						progressHUD.cancel();
					}
				}
			});
		}else{
			
			//没有网络就离线缓存

			//requestString = aCache.getAsJSONObject(CATEGORY_HTTP_REQUEST+params).toString();
			
			if(requestString!=null){
				
				 setListData(requestString);
			}
			if(progressHUD.isShowing()){
				progressHUD.cancel();
			}
		}
		
		return catgoryBeans;
	}
	
	private List<CatgoryBean>  setListData(String arg2){
		
		String requestString = arg2;

		System.out.println(requestString+"-------------------------");
		AllCatgoryRequestBean requestBeanRequest = new AllCatgoryRequestBean();

		//System.out.println("byte:"+new String(arg2));

		if(requestString!=null){
			
			requestBeanRequest = new Gson().fromJson(requestString, new TypeToken<AllCatgoryRequestBean>(){}.getType());

			if(requestBeanRequest.getState()==200){
				
				System.out.println(requestBeanRequest.getObjlistJson().get(0).getCname()+"**********************");
				
				
				catgoryBeans = requestBeanRequest.getObjlistJson();
				
				Intent intent = new Intent(DiscussSearchActivity.DISS_BISNUESS_RECEIVER);
				
				intent.putExtra("catgoryBeans", (Serializable)catgoryBeans);
				
				context.sendBroadcast(intent);
				
				return requestBeanRequest.getObjlistJson();

			}

		}
		
		return null;
		
	}
	



	
	class AllCatgoryRequestBean{
		private int state;

		private String message;

		List<CatgoryBean>objlistJson;

		
		public int getState() {
			return state;
		}

		public void setState(int state) {
			this.state = state;
		}

		public String getMessage() {
			return message;
		}

		public void setMessage(String message) {
			this.message = message;
		}

		public List<CatgoryBean> getObjlistJson() {
			return objlistJson;
		}

		public void setObjlistJson(List<CatgoryBean> objlistJson) {
			this.objlistJson = objlistJson;
		}

	}

}
